package cn.wizzer.app.web.commons.utils;

import cn.wizzer.app.sys.modules.services.SysIPTablesService;
import cn.wizzer.framework.base.model.IPRedisConst;
import cn.wizzer.framework.util.IPTableUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

@IocBean(name =  "iPTableUtil")
public class IPTableUtilImpl extends IPTableUtil {
    @Reference
    @Inject
    private SysIPTablesService sysIPTablesService;

    @Inject
    private RedisService redisService;

    @Override
    public boolean contains(String ip) {
        ipTables = sysIPTablesService.queryAll();
        if (super.contains(ip)) {
            return true;
        } else {
            // 将访问ip加入redis等待校验
            if (!IPRedisConst.LOCAL_IP.contains(ip)) {
                // 写个定时任务，5分钟跑一次，循环上限100次
                redisService.zadd(IPRedisConst.IPS, 1, ip);
            }
            return false;
        }
    }
}
